package com.vo
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.events.FlexEvent;
	import mx.graphics.codec.JPEGEncoder;
	
	import spark.filters.ColorMatrixFilter;

	[Bindable]
	public class PictureVO extends EventDispatcher
	{
		public function PictureVO()
		{
			super();
		}
		
		private var _url:String;
		public function set url( value:String ):void
		{
			if( bitmap == null )
			{
				_url = value;
				readImage();
			}
		}
		
		public function get url():String
		{
			return _url;
		}
		
		
		public var name:String;
		
		public var isSaved:Boolean = true;
		
		private var _bitmap:*;

		public function get bitmap():*
		{
			return _bitmap;
		}

		public function set bitmap(value:*):void
		{
			if( !value )
				return;
			_bitmap = value;
		}
		
//		private var _cropRec:Rectangle;
//
//		public function get cropRec():Rectangle
//		{
//			return _cropRec;
//		}
//
//		public function set cropRec(value:Rectangle):void
//		{
//			_cropRec = value;
//		}
		
		public function save():void
		{
		}
		
		public function reset():void
		{
		}
		
		public function convertBytes(bytesArray:ByteArray):void
		{
//			var loader:Loader = new Loader();
//			loader.contentLoaderInfo.addEventListener(Event.INIT,onLoadComplete);
//			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
//			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onErrorHandler);
//			loader.loadBytes(bytesArray);
			var bitmapData:BitmapData = new BitmapData(900,450);
			bytesArray.position = 0;
			trace(bytesArray.readObject());
			bitmap = new Bitmap(bitmapData);
		}
		
		private function onLoadComplete(event:Event):void
		{
			var loader:LoaderInfo = event.currentTarget as LoaderInfo;
			var w:Number = loader.width;
			var h:Number = loader.height;
			var bitmapData:BitmapData = new BitmapData(w,h);
			bitmapData.draw(loader.content);
			bitmap = new Bitmap(bitmapData);
		}
		
		private function onErrorHandler( event:Event ):void
		{
			Alert.show("转换失败!");
		}
		
		private function readImage():void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.INIT,onLoadComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onErrorHandler);
			loader.load(new URLRequest(url));
		}
		
		/**根据照片的尺寸来计算默认显示的大小*/
		private function caculateDisplaySize():void
		{
		}
		
		/**
		 *  照片显示在屏幕中的显示高度和宽度 
		 */		
		private var _displayWidth:Number;
		
		public function get displayWidth():Number
		{
			return _displayWidth;
		}

		public function set displayWidth(value:Number):void
		{
			_displayWidth = value;
		}

		
		private var _displayHeight:Number;

		public function get displayHeight():Number
		{
			return _displayHeight;
		}

		public function set displayHeight(value:Number):void
		{
			_displayHeight = value;
		}
		
		private var _index:int;

		public function get index():int
		{
			return _index;
		}

		public function set index(value:int):void
		{
			_index = value;
		}

		
	}
}